class HomeController < ApplicationController

  before_filter :check_auth

  def index
    @user = User.find(session[:uid])
    @profile = User.find(:all)
    @relationship = Relationship.find(:all, :conditions => ["user_id = '#{session[:uid]}' OR relation_id = '#{session[:uid]}' "])
  end
  
  def edit
    @user = User.find(session[:uid])
  end
  
  def update_profile
    @user = User.find(session[:uid])
    if @user.update_attributes(params[:user])
      flash[:notice] = "You have updated your profile"
      redirect_to :controller => "home", :action => "index"
    else
      flash[:error] = "Profile update failed."
    end
  end
  
  def profile
    @profile = User.find(params[:id])
  end
  
  def log_out
    session[:uid] = nil
    flash[:info] = 'You\'re logged out'
    redirect_to :controller => "main"
  end

  private

  def check_auth
    unless session[:uid]
      flash[:error] = 'Please login.'
      redirect_to :controller => "main", :action => "login"
    end
  end
end
